package com.poetize.plus.controller.admin;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.poetize.plus.entity.dto.FriendDTO;
import com.poetize.plus.entity.dto.page.FriendPageDTO;
import com.poetize.plus.entity.vo.FriendVO;
import com.poetize.plus.entity.vo.page.FriendPageVO;
import com.poetize.plus.service.FriendService;
import com.poetize.plus.utils.R;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import lombok.RequiredArgsConstructor;
import org.apache.ibatis.annotations.Update;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

/**
 * @Author zuosy
 * @Date 2024/5/14 17:07
 **/
@Tag(name = "友链信息")
@RestController
@RequestMapping("/admin/friend")
@RequiredArgsConstructor
public class FriendController {

    private final FriendService friendService;

    @GetMapping("/friendPage")
    @Operation(summary = "分页数据")
    @PreAuthorize(value = "hasRole('administrator')")
    public R<Page<FriendPageVO>> friendPage(FriendPageDTO friendPageDTO){
        return R.ok(friendService.friendPage(friendPageDTO));
    }

    @GetMapping("/{id}")
    @Operation(summary = "详情")
    @PreAuthorize(value = "hasRole('administrator')")
    public R<FriendVO> queryById(@PathVariable("id") Long id){
        return R.ok(friendService.queryById(id));
    }

    @PostMapping("/update")
    @Operation(summary = "修改")
    @PreAuthorize(value = "hasRole('administrator')")
    public R<Boolean> updateById(@RequestBody @Validated(Update.class)FriendDTO friendDTO){
        return R.ok(friendService.updateById(friendDTO));
    }

    @DeleteMapping("/delete/{id}")
    @Operation(summary = "删除")
    @PreAuthorize(value = "hasRole('administrator')")
    public R<Boolean> deleteById(@PathVariable("id") Long id){
        return R.ok(friendService.removeById(id));
    }

    @GetMapping("/changeStatus/{id}/{status}")
    @Operation(summary = "修改审核状态")
    @PreAuthorize(value = "hasRole('administrator')")
    public R<Boolean> changeStatus(@PathVariable("id") Long id,
                                   @PathVariable("status") Boolean status){
        return R.ok(friendService.changeStatus(id, status));
    }
}
